package upgrade

import (
	"context"
	"crypto/sha256"
	"fmt"
	"os"
	"strings"

	"github.com/gogf/gf/v2/crypto/gmd5"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/sagoo-cloud/sagooiot/internal/consts"
	"github.com/sagoo-cloud/sagooiot/internal/dao"
	"github.com/sagoo-cloud/sagooiot/internal/model"
	"github.com/sagoo-cloud/sagooiot/internal/service"
)

type sUpgradeDevOtaFireware struct{}

func sUpgradeDevOtaFirewareNew() *sUpgradeDevOtaFireware {
	return &sUpgradeDevOtaFireware{}
}
func init() {
	service.RegisterUpgradeDevOtaFireware(sUpgradeDevOtaFirewareNew())
}

// GetDevOtaFirewareList 获取列表数据
func (s *sUpgradeDevOtaFireware) GetDevOtaFirewareList(ctx context.Context, in *model.GetDevOtaFirewareListInput) (total, page int, list []*model.DevOtaFirewareOutput, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		m := dao.DevOtaFireware.Ctx(ctx).InnerJoin("dev_product", "dev_product.id=dev_ota_fireware.product_id").InnerJoin("dev_ota_module", "dev_ota_module.id = dev_ota_fireware.module")
		total, err = m.Count()
		if err != nil {
			err = gerror.New("获取总行数失败")
			return
		}
		page = in.PageNum
		if in.PageSize == 0 {
			in.PageSize = consts.PageSize
		}
		err = m.Fields("dev_ota_fireware.*,dev_product.name productName,dev_ota_module.name module_name").
			Page(page, in.PageSize).Fields("dev_ota_fireware.*,dev_product.name product_name").Order("dev_ota_fireware.created_at desc").Scan(&list)
		if err != nil {
			err = gerror.New("获取数据失败")
		}
	})
	return
}

// GetDevOtaFirewareById 获取指定ID数据
func (s *sUpgradeDevOtaFireware) GetDevOtaFirewareById(ctx context.Context, id int) (out *model.DevOtaFirewareOutput, err error) {
	err = dao.DevOtaFireware.Ctx(ctx).Where(dao.DevOtaFireware.Columns().Id, id).LeftJoin("dev_product", "dev_product.id=dev_ota_fireware.product_id").
		LeftJoin("dev_ota_module", "dev_ota_module.id = dev_ota_fireware.module").
		Fields("dev_ota_fireware.*,dev_product.name productName,dev_ota_module.name module_name").Scan(&out)
	return
}

// AddDevOtaFireware 添加数据
func (s *sUpgradeDevOtaFireware) AddDevOtaFireware(ctx context.Context, in model.DevOtaFirewareAddInput) (err error) {
	in.CreatedBy = service.Context().GetUserId(ctx)
	arr := strings.Split(in.Url, "/")
	if len(arr) == 6 {
		path := strings.Join(arr[3:], "/")
		bs, err := os.ReadFile("./" + path)
		if in.Are == "SHA256" {
			sum := sha256.Sum256(bs)
			in.Sign = fmt.Sprintf("%x", sum) // 16进制
		}
		if in.Are == "MD5" {
			if err == nil {
				in.Sign = gmd5.MustEncryptBytes(bs) // 16进制
			}
		}

	}
	_, err = dao.DevOtaFireware.Ctx(ctx).Insert(in)
	return
}

// EditDevOtaFireware 修改数据
func (s *sUpgradeDevOtaFireware) EditDevOtaFireware(ctx context.Context, in model.DevOtaFirewareEditInput) (err error) {
	dao.DevOtaFireware.Ctx(ctx).FieldsEx(dao.DevOtaFireware.Columns().Id).Where(dao.DevOtaFireware.Columns().Id, in.Id).OmitEmpty().Update(in)
	return
}

// DeleteDevOtaFireware 删除数据
func (s *sUpgradeDevOtaFireware) DeleteDevOtaFireware(ctx context.Context, Ids []int) (err error) {
	_, err = dao.DevOtaFireware.Ctx(ctx).Delete(dao.DevOtaFireware.Columns().Id+" in (?)", Ids)
	return
}
